`timescale  1ns/1ns
module tb_RGB2HSV();

localparam image_width  = 1024;
localparam image_height = 600;
//----------------------------------------------------------------------
//  clk & rst_n
reg                             clk;
reg                             rst_n;

initial
begin
    clk = 1'b0;
    forever #5 clk = ~clk;
end

initial
begin
    rst_n = 1'b0;
    repeat(50) @(posedge clk);
    rst_n = 1'b1;
end

//----------------------------------------------------------------------
//  Image data prepred to be processed
reg                             per_img_vsync;
reg                             per_img_href ;
reg                             per_img_valid;
reg             [7:0]           rgb_r;
reg             [7:0]           rgb_g;
reg             [7:0]           rgb_b;

wire                            post_img_vsync;
wire                            post_img_href ;
wire                            post_img_valid;
wire             [8:0]          hsv_h;
wire             [8:0]          hsv_s;
wire             [7:0]          hsv_v;                         

//  Image data has been processed
//----------------------------------------------------------------------
//  task and function
task image_input;
    bit             [31:0]      row_cnt;
    bit             [31:0]      col_cnt;
    bit             [7:0]       mem     [image_width*image_height-1:0];
    $readmemh("D:/Matlab/Matlab_Projects/2_Matlab_Project/2_VIP_RGB888_to_YCbCr444/img_RGB.dat",mem);
    
    for(row_cnt = 0;row_cnt < image_height;row_cnt++)
    begin
        repeat(5) @(posedge clk);
        per_img_vsync = 1'b0;
        repeat(5) @(posedge clk);
        for(col_cnt = 0;col_cnt < image_width;col_cnt++)
        begin
            per_img_valid = ~per_img_valid;
            per_img_href  = 1'b1;
            rgb_r = mem[(row_cnt*image_width+col_cnt)*3+0];
            rgb_g = mem[(row_cnt*image_width+col_cnt)*3+1];
            rgb_b = mem[(row_cnt*image_width+col_cnt)*3+2];
            @(posedge clk);
        end
        per_img_href  = 1'b0;
    end

    per_img_vsync = 1'b1;
    @(posedge clk);
    
endtask : image_input

RGB2HSV  u_RGB2HSV (
    .clk                     (clk          ),
    .reset_n                 (rst_n      ),
    .per_img_vsync           (per_img_vsync),
    .per_img_href            (per_img_href ),
    .per_img_valid           (1'd1),
    .rgb_r                   (rgb_r),
    .rgb_g                   (rgb_g),
    .rgb_b                   (rgb_b),

    .post_img_vsync          (post_img_vsync),
    .post_img_href           (post_img_href ),
    .post_img_valid          (post_img_valid),
    .hsv_h                   (hsv_h),
    .hsv_s                   (hsv_s),
    .hsv_v                   (hsv_v)
);

initial
begin
    per_img_vsync = 0;
    per_img_href  = 0;
    per_img_valid = 0;
    rgb_r = 0;    
    rgb_g = 0;    
    rgb_b = 0;
end

initial 
begin
    wait(rst_n);
    repeat(5) @(posedge clk); 
    
    image_input;
    repeat(1000) @(posedge clk); 

    $stop;
end 

endmodule